package com.joelbitar.switchctrl;

/* Devicetypes that shold be suported
 * 
 * A bunch of diferent lights
   Light
   Lavalamp
   Office light
   Living room light
 * TV
 * Stereo
 * Air condition
 * Heater
 * Fridge
 * Car-heater
 * Lawn mower
 * Phone
 * Server
 * Christmas tree
 * 
 * */

import android.database.Cursor;

public class Device {
	public static final int TYPE_SWITCH = 0;
	public static final int TYPE_DIMMER = 1;
	public static final int DEFAULT_TYPE = TYPE_SWITCH;
	public static final int SWITCH_MARKED_NONE = -1;
	public static final int SWITCH_MARKED_ON = 1;
	public static final int SWITCH_MARKED_OFF = 0;
	public static final int STATUS_NOT_KNOWN = -1;
	public static final int REQUEST_STATUS_NONE = -1;
	public static final int REQUEST_STATUS_OFF = 0;
	public static final int REQUEST_STATUS_ON = 1;
	
	// Internal ID, in database and such.
	private long 		id;
	// id on the server, used in sending data
	private int			device_id;
	private String 		name;
	// Last known status
	// Status can be described as 1 or 0 for a binary device, or 0 - 100 for a non-binary device
	private int 		status;
	// If the request is in progress (As in sent to server)
	private boolean		requestInProgress 	= false;
	
	// If request in progress is true, the status that was sent could and should be found here.
	// The REAL status should be set After the request is finished
	private int			requestStatus		= REQUEST_STATUS_NONE;
	

	// Server this device is on
	private Server 		server;
	// What type of device, dimmer, switch, used for sending data
	private int			type;
	// What the device is marked as, default to none.
	private int 		marked 				= SWITCH_MARKED_NONE;
	// What icon to use. Also is related to what type of message to send
/*
   If icon is a sort of lamp, should prompt with one message
   If icon is some sort of device, should promt with anohter message
*/
	private int 		icon 				= 0;
	// Short description, NOT provided by server
	private String		description;
	
	/*
	 * Public variables for use in the view
	 * */
	public int			view_marked_off 	= -1;
	public int			view_marked_on 		= -1;
	public boolean		view_loading_animation_running = false;
	public int			view_icon_resource 	= -1;
	public int 			view_request_status_color = -1;
	
	public int getIcon() {
		return icon;
	}
	public void setIcon(int icon) {
		this.icon = icon;
	}
	
	public int getMarked() {
		return marked;
	}
	public void setMarked(int marked) {
		this.marked = marked;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	
	public int getDevice_id() {
		return device_id;
	}
	public void setDevice_id(int deviceId) {
		device_id = deviceId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public int getStatus() {
		return status;
	}
	public int isOn() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	
	public Server getServer() {
		return server;
	}
	public void setServer(Server server) {
		this.server = server;
	}
	
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	

	public int getIconResource()
	{
		int r = 0;

    	switch(icon)
    	{
		default: // Case 1...
			if(status == 1)
				r = R.drawable.light_on;
			else
				r = R.drawable.light_off;
			break;
    	}
        
        return r;
	}
	
	public void setDesc(String description) {
		this.description = description;
	}
	
	public String getDesc() {
		return description;
	}
	
	public void setFromCursor(Cursor cursor) {
		this.setId(cursor.getInt(DeviceDbAdapter.ID_COLUMN));
		this.setDevice_id(cursor.getInt(DeviceDbAdapter.DEVICE_ID_COLUMN));
		this.setName(cursor.getString(DeviceDbAdapter.NAME_COLUMN));
		this.setDesc(cursor.getString(DeviceDbAdapter.DESC_COLUMN));
		this.setStatus(cursor.getInt(DeviceDbAdapter.STATUS_COLUMN));
		this.setIcon(cursor.getInt(DeviceDbAdapter.ICON_COLUMN));
	}
	public void setRequestInProgress(boolean requestInProgress) {
		this.requestInProgress = requestInProgress;
	}
	public boolean getRequestInProgress() {
		return requestInProgress;
	}

	public int getRequestStatus() {
		return requestStatus;
	}
	public void setRequestStatus(int requestStatus) {
		this.requestStatus = requestStatus;
	}
	
	// Returns the text resource int
	public int getRequestInProgressTextResource() {
		if(this.getRequestInProgress() == false)
		{
			return -1;
		}
		else
		{
			switch(this.icon)
			{
			// If a normal device
			default: // Also case 1 if anyone asks...
				if(this.type == Device.TYPE_SWITCH)
				{
					// IT's a switch
					if(this.getRequestStatus() == Device.REQUEST_STATUS_OFF)
					{
						return R.string.message_device_turning_off;
					}
					else
					{
						return R.string.message_device_turning_on;
					}
				}
				else
				{
					// IT's a dimmer
					if(this.getRequestStatus() == 0)
					{
						return R.string.message_device_turning_off;
					}
					else
					{
						return R.string.message_device_setting_to;
					}
					
				}
			}
		}
	}
}
